package com.atos.smp.actions;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

import org.primefaces.component.commandlink.CommandLink;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@SessionScoped
@ManagedBean
public class TreeBean implements Serializable {

	private TreeNode root;
	private Map<String, String> nodes ;
	private TreeNode selectedNode;
	
	public TreeBean() {
		nodes = new HashMap<String, String>();
		nodes.put("LIST_COLLABORATOR", "collaboratorList.xhtml");
		nodes.put("NEW_COLLABORATOR", "addCollaborator.xhtml");

		nodes.put("LIST_PROJECT", "projectList.xhtml");
		nodes.put("NEW_PROJECT", "addProject.xhtml");

		nodes.put("LIST_TOOL", "toolList.xhtml");
		nodes.put("NEW_TOOL", "addTool.xhtml");
		
		root = new DefaultTreeNode("Root", null);
		TreeNode gp = new DefaultTreeNode("PROJECT_MANAGEMENT", root);
		gp.setSelectable(false);
		TreeNode addGP = new DefaultTreeNode("NEW_PROJECT", gp);
		TreeNode listGP = new DefaultTreeNode("LIST_PROJECT", gp);
		
		
		TreeNode gc = new DefaultTreeNode("COLLABORATORS_MANAGEMENT", root);
		gc.setSelectable(false);
		TreeNode addGC = new DefaultTreeNode("NEW_COLLABORATOR", gc);
		TreeNode listGC = new DefaultTreeNode("LIST_COLLABORATOR", gc);
		
		TreeNode gt = new DefaultTreeNode("TOOLS_MANAGEMENT", root);
		gc.setSelectable(false);
		TreeNode addGT = new DefaultTreeNode("NEW_TOOL", gt);
		TreeNode listGT = new DefaultTreeNode("LIST_TOOL", gt);

		
	}

    public TreeNode getRoot() {  
        return root;  
    }  
  
    public TreeNode getSelectedNode() {  
        return selectedNode;  
    }  
  
    public void setSelectedNode(TreeNode selectedNode) {  
        this.selectedNode = selectedNode;  
    }  
  
  
    public void onNodeSelect(NodeSelectEvent event) {         
        try {
        	String link = nodes.get(event.getTreeNode().toString());
        	if(link != null)
			FacesContext.getCurrentInstance().getExternalContext().redirect(link);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }  
  
 
}